package com.jackrain.nea.fc.vp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.fc.vp.api.ViewCinteadjQueryCmd;
import com.jackrain.nea.fc.vp.mapper.ViewCinteadjMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * VIP积分调整service
 * FC新增“积分调整单”—> F4
 *
 * @author wuwenxue
 * @date 2017/11/17
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "vp")
public class ViewCinteadjQueryCmdImpl extends CommandAdapter implements ViewCinteadjQueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

//        log.info("<=============VIP积分调整调用==============>");
//        log.info("ViewCinteadjQueryCmdImpl-->start");

        ViewCinteadjMapper viewCinteadjMapper = ApplicationContextHandle.getBean(ViewCinteadjMapper.class);
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),
                "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCinteadjQueryCmdImpl--param-->{}",param);

        if ((null == param) || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] id = param.getJSONArray("IDS").toArray();
        JSONArray jsonArray = new JSONArray();

        List<HashMap> list = viewCinteadjMapper.findAllInteadj(id);
        for (HashMap<String, Object> hashMap : list) {
            //VIP
            Long vipID = TypeUtils.castToLong(hashMap.get("VP_C_VIP_ID"));
            //VIP类型
            Long viptypegroupID = TypeUtils.castToLong(hashMap.get("VP_C_VIPTYPEGROUP_ID"));

            //VIP卡号
            String cardID = viewCinteadjMapper.vipEcode(vipID, viptypegroupID);
            //卡类型编号
            String viptypeID = viewCinteadjMapper.vipType(viptypegroupID);

            if (!("FF".equals(viptypeID))) {
//                log.info("卡类型：" + viptypeID);
                throw new NDSException("卡类型不符！");
            }

            //积分变动
            BigDecimal vipScore = TypeUtils.castToBigDecimal(hashMap.get("INTEADJ"));
            //积分时间
            Date statusTime = TypeUtils.castToDate(hashMap.get("STATUSTIME"));

            /*Date scoreDate = TypeUtils.castToDate(hashMap.get("BILLDATE").toString());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sc = simpleDateFormat.format(scoreDate);
            log.info("积分时间" + scoreDate);*/
            //积分类型
            BigDecimal scoreType = new BigDecimal(2);
            //积分来源说明
            String scoreRemark = TypeUtils.castToString(hashMap.get("REMARK"));
            //参照单号
            String billCzid = TypeUtils.castToString(hashMap.get("DOCNO"));

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("VIPNO", cardID);
            jsonObject.put("VP_C_VIPTYPEGROUP_ID", viptypeID);
            jsonObject.put("INTEADJ", vipScore);
            jsonObject.put("STATUSTIME", statusTime);
            jsonObject.put("SCORETYPE", scoreType);
            jsonObject.put("REMARK", scoreRemark);
            jsonObject.put("DOCNO", billCzid);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject,
                    "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);
        }
        vh.put("data", jsonArray);

        return vh;
    }

}
